package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.PlaceDTO;
import net.wanho.po.Place;
import net.wanho.service.PlaceService;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.PageInfo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/place")
public class PlaceController {

    private final PlaceService placeService;

    @GetMapping("/place-list")
    public AjaxResult placeList() {
        return AjaxResult.success(placeService.list());
    }

    @GetMapping("/place-page-list")
    public AjaxResult placePageList(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "5") int pageSize,
                                    PlaceDTO placeDTO) {
        PageInfo<Place> pageInfo = placeService.pageList(pageNum, pageSize, placeDTO);

        return AjaxResult.success("success", pageInfo);
    }

    @GetMapping("/place-num-check")
    public AjaxResult placeNumCheck(@RequestParam String placeNum) {
        Boolean placeNumCheck = placeService.selectIdByPlaceName(placeNum);
        return AjaxResult.success("success",placeNumCheck);
    }

    @PostMapping("/place-add")
    public AjaxResult placeAdd(@RequestBody Place place) {
        place.setIsDelete("0");
        placeService.save(place);
        return AjaxResult.success("产地添加成功");
    }

    @PutMapping("/place-update")
    public AjaxResult placeUpdate(@RequestBody Place place) {
        placeService.updateById(place);
        return AjaxResult.success("产地修改成功");
    }

    @DeleteMapping("/place-delete/{id}")
    public AjaxResult placeDelete(@PathVariable Integer id) {
        placeService.removeById(id);
        return AjaxResult.success("产地删除成功");
    }

    @GetMapping("/exportTable")
    public AjaxResult exportTable(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "5") int pageSize,
                                  PlaceDTO placeDTO) {
        PageInfo<Place> pageInfo = placeService.pageList(pageNum, pageSize, placeDTO);
        return AjaxResult.success("success", pageInfo);
    }
}
